package cc.mrbird.febs.customer.controller;


import cc.mrbird.febs.common.annotation.ControllerEndpoint;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.customer.entity.Activity;
import cc.mrbird.febs.customer.entity.Share;
import cc.mrbird.febs.customer.service.IActivityService;
import cc.mrbird.febs.customer.service.IShareService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xdgf.usermodel.shape.ShapeRenderer;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Macw
 * @since 2020-11-04
 */
@RestController
@RequestMapping("/share")
public class ShareController extends BaseController {

    @Autowired
    private IShareService shareService;

    @Autowired
    private IActivityService activityService;

    /**
     * @Author wangke
     * @Description 获取分享列表
     * @Date 18:29 2020/11/6
     * @Param 
     * @return 
     */
    @RequestMapping("getShareList")
    @ControllerEndpoint(operation = "查询活动", exceptionMessage = "查询活动失败")
    public FebsResponse getShareList(Share share, QueryRequest queryRequest){
        Page<Share> page = new Page<>(queryRequest.getPageNum(), queryRequest.getPageSize());
        QueryWrapper<Share> wrapper = new QueryWrapper<>();
        wrapper.eq("status",1)
                .like(StringUtils.isNotBlank(share.getTitle()), "title",share.getTitle())
                .orderByDesc("create_time");
        IPage<Share> page1 = shareService.page(page, wrapper);
        return new FebsResponse().success().data(getDataTable(page1));
    }

    @PostMapping("/updateShare")
    @RequiresPermissions("share:view")
    @ControllerEndpoint(operation = "修改分享", exceptionMessage = "修改分享")
    public FebsResponse updateShare(@Valid Share share, @RequestParam("headPhoto_file") MultipartFile headPhoto_file) {
        try{
            if (headPhoto_file != null && headPhoto_file.getSize() > 0) {
                String basePath = "share";
                String url = "";
                url =  aliOSSUpload(headPhoto_file, basePath);
                //获取上传文件的路径
                //将文件大小，和文件上传后返回的路径，入库
                share.setImgUrl(url);
            }
            shareService.updateById(share);
            return new FebsResponse().success();
        }catch(Exception e){
            return new FebsResponse().fail();
        }
    }
}
